package com.example.teesystem.common.utils;

import com.example.teesystem.entity.LoginInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @author pxr
 * @data
 */
public class LoginUtil {
    private static final int MAX_LOGIN_ERROR_TIMES=5;
    private static final int ACCOUNT_FREEZE_DURATION = 300;
    //保存用户的登录信息
    private static final Map<String, LoginInfo> loginInfoHashMap=new HashMap<String,LoginInfo>();

    /**
     * 将某个uid的salt保存进登录信息
     * @param uid 用户id
     * @param salt 生成的盐
     */
    public static boolean setSaltByUid(int uid ,String salt) {
        LoginInfo loginInfo = getLoginInfoByUid(uid);
        if (loginInfo == null) {
            loginInfo = new LoginInfo(salt);
        } else if (loginInfo.getErrorLoginTimes() >= MAX_LOGIN_ERROR_TIMES &&
                TimeUtil.getSecondTimestamp() - loginInfo.getLastLoginTime() <= ACCOUNT_FREEZE_DURATION) {
            //账户被锁定后，不插入盐
            return false;
        } else if (TimeUtil.getSecondTimestamp() - loginInfo.getLastLoginTime() >= ACCOUNT_FREEZE_DURATION) {
            //当时间达到锁定时间之后，将重置登录失败信息
            loginInfo = new LoginInfo(salt);
        } else {
            //未锁定的情况下不更新
            loginInfo.setSalt(salt);
            loginInfo.setLastLoginTime(TimeUtil.getSecondTimestamp());
        }

        loginInfoHashMap.put(String.valueOf(uid), loginInfo);
        return true;

    }

    /**
     *
     */
    public static boolean login(int uid,String password,String passwordMD5){
        LoginInfo loginInfo = getLoginInfoByUid(uid);
        if(loginInfo==null) {
            return false;
        }
        if (loginInfo.getErrorLoginTimes() >= MAX_LOGIN_ERROR_TIMES &&
                TimeUtil.getSecondTimestamp() - loginInfo.getLastLoginTime() <= ACCOUNT_FREEZE_DURATION) {
            //账户被锁定后,不能验证登录
            System.out.println("账户已被禁用，登录失败次数" + loginInfo.getErrorLoginTimes()+"时间差"+String.valueOf(TimeUtil.getSecondTimestamp() - loginInfo.getLastLoginTime()));
            return false;
        }
        String realPasswordMD5 = null;
        try {
            realPasswordMD5 = MD5Util.getMD5(loginInfo.getSalt()+password);
        } catch (Exception e) {
            return false;
        }
        if(realPasswordMD5.equals(passwordMD5)){
            //验证成功后清楚登录相关信息
            deleteLoginInfoByUid(uid);
            return true;
        }

        //登录失败后上一次登录时长大于五分钟就先清空然后写入登录失败信息
        if(TimeUtil.getSecondTimestamp() - loginInfo.getLastLoginTime() >= ACCOUNT_FREEZE_DURATION) {
            loginInfo=new LoginInfo();
        }
        //密码验证失败时将登录错误信息更新
        loginInfo.setLastLoginTime(TimeUtil.getSecondTimestamp());
        loginInfo.setErrorLoginTimes(loginInfo.getErrorLoginTimes()+1);

        //记录登录失败次数大于或者等于五次的情况
        if(loginInfo.getErrorLoginTimes()>=MAX_LOGIN_ERROR_TIMES){
            logLoginError(uid);
        }

        loginInfoHashMap.put(String.valueOf(uid), loginInfo);

        return false;
    }

    private static void logLoginError(int uid) {
        //Todo

    }


    /**
     * 根据用户id返回对应身份信息
     */
    private static LoginInfo getLoginInfoByUid(int uid){

        if(!loginInfoHashMap.containsKey(String.valueOf(uid))) {
            return null;
        } else {
            return loginInfoHashMap.get(String.valueOf(uid));
        }
    }

    /**
     * 删除登录相关信息
     */
    private static void deleteLoginInfoByUid(int uid){
        if(!loginInfoHashMap.containsKey(String.valueOf(uid))) {
            return;
        }
        loginInfoHashMap.remove(String.valueOf(uid));
    }

}
